﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;

namespace RedemptoristRetreatCenter.Account
{
    /// <summary>
    /// The UserRegistration class allows users to create an account that will allow them to login to the website
    /// </summary>
     partial class UserRegistration : Page
      {
         /// <summary>
         /// Upon page load, a list of states is bound to the state dropdown menu 
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            StateAbbreviations states = new StateAbbreviations();
            state.DataSource = states.getListOfStates();
            state.DataBind();
        }
        
         /// <summary>
         /// Determine whether a username already exists within the database
         /// </summary>
         /// <param name="username"></param>
         /// <returns>Boolean</returns>
        private Boolean doesUserExist(String username)
        {
            UserAccountUtility userAccountUtil = new RedemptoristRetreatCenter.DatabaseUtility.UserAccountUtility();

            return userAccountUtil.userExists(username);
        }

        /// <summary>
        /// Determines whether the password and confirmation password are identical
        /// </summary>
        /// <param name="password1"></param>
        /// <param name="password2"></param>
        /// <returns>Boolean</returns>
        private Boolean passwordsMatch(String password1, String password2)
        {
            return password1.Equals(password2);
        }

        /// <summary>
        /// Upon the user entering their choosen username, verifies that the username doesn't already exist
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Username_Change(object sender, EventArgs e)
        {
            if (doesUserExist(UserName.Text))
                ErrorMessage.Text = "Username already exists. Please choose a new username";
        }

         /// <summary>
         /// Upon the user entering the password and confirmation password, verifies that the passwords match
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
        protected void Password_ValidateMatch(object sender, EventArgs e)
        {
            if (!(passwordsMatch(Password.Text, ConfirmPassword.Text)))
                ErrorMessage.Text = "These passwords do not match. Please re-enter your password";
        }

        /// <summary>
        /// Upon clicking the register button, verifies usability of username, password and carries out registration process accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Register_Click(object sender, EventArgs e)
        {
            UserAccountUtility userAccountUtil = new RedemptoristRetreatCenter.DatabaseUtility.UserAccountUtility();

            if (doesUserExist(UserName.Text))
                ErrorMessage.Text = "Username already exists. Please choose a new username";
            else if (!(passwordsMatch(Password.Text, ConfirmPassword.Text)))
                ErrorMessage.Text = "These passwords do not match. Please re-enter your password";
            else
            {
                UserAccountInfo newAccount = new UserAccountInfo(UserName.Text, Password.Text, false, false, false, 
                                                                    firstName.Text, lastName.Text, addressLineOne.Text, addressLineTwo.Text, city.Text, state.Text, 
                                                                    zipCode.Text, homePhone.Text, cellPhone.Text, businessPhone.Text, Email.Text, null, 
                                                                    UserName.Text, System.DateTime.Now);
                if (userAccountUtil.insertUserAccount(newAccount, UserName.Text))
                    ErrorMessage.Text = "Your account has been created!";
                else
                    ErrorMessage.Text = "Uh oh, there was a problem creating your account.";
            }


        }
     }
 }